package com.eastfair.home.service.impl;

import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.service.HomeDrawingService;
import com.eastfair.home.vo.ReportVenueDrawingCategoryVO;
import com.eastfair.venuebooking.api.DrawingFeign;
import com.eastfair.venueservice.dto.DrawingDTO;
import com.eastfair.venueservice.vo.DrawingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 综合服务图纸类型服务.
 *
 * @author dqq
 * @date 2022 /10/28
 */
@Service
@Slf4j
public class HomeDrawingServiceImpl implements HomeDrawingService {

    @Resource
    private DrawingFeign drawingFeign;

    @Override
    public List<DrawingVO> getDrawingList(DrawingDTO drawingPageQuery) {
        log.info("getDrawingList - 图纸列表, drawingPageQuery={}", drawingPageQuery);
        R<List<DrawingVO>> result = drawingFeign.getDrawingList(drawingPageQuery);
        log.info("getDrawingList - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public List<DrawingVO> listVOByExhibitionId(Long exhibitionId, String spaceCode) {
        log.info("homeDrawingService - 展会ID查询综合服务图纸分类列表, exhibitionId={}", exhibitionId);
        DrawingDTO param = new DrawingDTO();
        param.setExhibitionId(exhibitionId);
        param.setSpaceCode(spaceCode);
        param.setIsEnabled(BusinessConstant.ENABLE_YES);
        return getDrawingList(param);
    }

    @Override
    public List<ReportVenueDrawingCategoryVO> listReportVenueDrawingCategoryByExhibitionId(Long exhibitionId) {
        log.info("homeDrawingService - 展会ID查询综合服务图纸分类列表, exhibitionId={}", exhibitionId);
        DrawingDTO param = new DrawingDTO();
        param.setExhibitionId(exhibitionId);
        param.setIsEnabled(BusinessConstant.ENABLE_YES);
        List<DrawingVO> drawingList = getDrawingList(param);
        if (drawingList == null || drawingList.isEmpty()) {
            return Collections.emptyList();
        }
        return drawingList
                .stream()
                .map(drawingVO -> {
                    ReportVenueDrawingCategoryVO drawingCategoryVO = new ReportVenueDrawingCategoryVO();
                    drawingCategoryVO.setDrawingCategoryId(drawingVO.getId());
                    drawingCategoryVO.setDrawingCategoryName(drawingVO.getDrawingName());
                    drawingCategoryVO.setDrawingCategoryType(drawingVO.getDrawingType());
                    drawingCategoryVO.setDrawingCategorySort(drawingVO.getSort());
                    return drawingCategoryVO;
                })
                .collect(Collectors.toList());
    }
}